The other day I needed to host a compiled Angular application in a pipeline (to run UI test against it). I wanted this to be a C# program, because that is what I am familiar with, and I need to control the port it is running on with a environment variable.

This turned out to be very simple in the end, but took some searching and expirimenting, so documenting it here for the next guy.

Ended up with only this one simple class:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSpaStaticFiles(conf => { conf.RootPath = "wwwroot"; });
var app = builder.Build();
app.UseSpaStaticFiles();
app.UseSpa(spa => { spa.Options.DefaultPage = "/index.html"; } );
app.UseDefaultFiles();
app.Run();

If you place your Angular build files under the wwwroot folder and run this program, this will work for Angular.

If you want to vary in settings based on the ASPNETCORE_ENVIRONMENT Environment variable, you can use different appsettings files, for example:

appsettings.Development.json
appsettings.Docker.json

For controlling the hosting port I use this setting there:

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:4201"
      }
    }
  }
}

Hope this helps.